﻿using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Ioc;
using JiveMessenger.Interfaces;
using JiveMessenger.User;
using JmShared;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Data;

namespace JiveMessenger.ViewModels
{
    public class ViewModelNotificationsPage : ViewModelBase, IViewModelNotificationsPage
    {
        public RelayCommand AcceptInviteCommand { get; set; }
        public RelayCommand DeclineInviteCommand { get; set; }

        private ObservableCollection<INotification> _notifications;
        public ObservableCollection<INotification> Notifications
        {
            get { return _notifications; }
            set
            {
                if (_notifications != value)
                {
                    if (_notifications != null)
                        _notifications.CollectionChanged -= Notifications_CollectionChanged;
                    Set(() => Notifications, ref _notifications, value);
                    if (_notifications != null)
                        _notifications.CollectionChanged += Notifications_CollectionChanged;
                }
            }
        }


        private bool _haveUnreadNotifications;
        public bool HaveUnreadNotifications
        {
            get { return _haveUnreadNotifications; }
            set
            {
                if (_haveUnreadNotifications != value)
                {
                    Set(() => HaveUnreadNotifications, ref _haveUnreadNotifications, value);
                }
            }
        }

        /// <summary>
        /// Initializes a new instance of the ViewModelNotificationsWindow class.
        /// </summary>
        public ViewModelNotificationsPage()
        {

            this.Notifications = new ObservableCollection<INotification>();
            ICollectionView view = null;// CollectionViewSource.GetDefaultView(this.Notifications) as ICollectionView;
            view.GroupDescriptions.Add(new PropertyGroupDescription("NotificationType"));
            this.AcceptInviteCommand = new RelayCommand(AcceptInviteCommandAction, InviteCommandCanExecute);
            this.DeclineInviteCommand = new RelayCommand(DeclineInviteCommandAction, InviteCommandCanExecute);
        }
        private bool InviteCommandCanExecute()
        {
            return this.Notifications.Any(x => x.IsSelected);
        }
        private void DeclineInviteCommandAction()
        {
            var selectedNotifications = new List<INotification>(this.Notifications.Where(x => x.IsSelected));
            foreach (INotification notification in selectedNotifications)
            {
                if (notification.NotificationType == NotificationType.FriendInvite)
                    App.Connection.SendMessage(new ChatMessage(Client.Instance.CurrentUserInfo.UserID, MessageType.DeclineFriendRequest) { RecipientID = notification.SenderUsername });
                else if (notification.NotificationType == NotificationType.GroupInvite)
                    App.Connection.SendMessage(new ChatMessage(Client.Instance.CurrentUserInfo.UserID, MessageType.DeclineGroupInvite) { RecipientID = ((GroupInviteNotification)notification).Group.ID });
                this.Notifications.Remove(notification);

            }
            Client.Instance.CurrentUserInfo.Notifications = this.Notifications.ToArray();
        }
        private void AcceptInviteCommandAction()
        {
            var selectedNotifications = new List<INotification>(this.Notifications.Where(x => x.IsSelected));
            foreach (INotification notification in selectedNotifications)
            {
                if (notification.NotificationType == NotificationType.FriendInvite)
                    App.Connection.SendMessage(new ChatMessage(Client.Instance.CurrentUserInfo.UserID, MessageType.AcceptFriendRequest) { RecipientID = notification.SenderUsername });
                else if (notification.NotificationType == NotificationType.GroupInvite)
                    App.Connection.SendMessage(new ChatMessage(Client.Instance.CurrentUserInfo.UserID, MessageType.AcceptGroupInvite) { RecipientID = ((GroupInviteNotification)notification).Group.ID });
                this.Notifications.Remove(notification);
            }
            Client.Instance.CurrentUserInfo.Notifications = this.Notifications.ToArray();
        }

        void Notifications_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            this.HaveUnreadNotifications = ((ObservableCollection<INotification>)sender).Any(x => !x.IsRead);
            RaisePropertyChanged("Notifications");
            //this.NotificationsBarText = SetNotificationBarText();
        }

        public override void Cleanup()
        {
            SimpleIoc.Default.Unregister<ViewModelNotificationsPage>(ViewModelLocator.ViewModelNotificationsWindowKey);
            ViewModelLocator.ViewModelNotificationsWindowKey = System.Guid.NewGuid().ToString();
            this.Notifications.CollectionChanged -= Notifications_CollectionChanged;
            this.Notifications = null;
            base.Cleanup();
        }
    }
}
